package com.xiaoma.thread.condition;

import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

public class Main {

  public static void main(String[] args) {
    Lock lock = new ReentrantLock();

    Condition threadOne = lock.newCondition();
    Condition threadTwo = lock.newCondition();
    Condition threadThree = lock.newCondition();

    new Thread(() -> {
      while (true) {
        lock.lock();
        threadTwo.signal();
        System.out.println(Thread.currentThread().getName() + "：A");
        try {
          threadOne.await();
        } catch (InterruptedException e) {
          e.printStackTrace();
        } finally {
          lock.unlock();
        }
      }
    }, "thread-1").start();

    new Thread(() -> {
      while (true) {
        lock.lock();
        threadThree.signal();
        System.out.println(Thread.currentThread().getName() + "：B");
        try {
          threadTwo.await();
        } catch (InterruptedException e) {
          e.printStackTrace();
        } finally {
          lock.unlock();
        }
      }
    }, "thread-2").start();


    new Thread(() -> {
      while (true) {
        lock.lock();
        threadOne.signal();
        System.out.println(Thread.currentThread().getName() + "：c");
        try {
          threadThree.await();
        } catch (InterruptedException e) {
          e.printStackTrace();
        } finally {
          lock.unlock();
        }
      }
    }, "thread-3").start();
  }
}
